//
//  SEGActivityMyUploadCell.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/8/10.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGActivityMyUploadCell.h"

#if __has_include(<Commom/SEGColorManager.h>)
#import <Commom/SEGColorManager.h>
#else
#import "SEGColorManager.h"
#endif

@interface SEGActivityMyUploadCell ()

@property (nonatomic, strong) UILabel* timeLabel; //时间
@property (nonatomic, strong) UILabel* contentLabel; //内容
@property (nonatomic, strong) UIView *imageContainer; //图片容器

/**
 图片url数组
 */
@property (nonatomic, strong)NSMutableArray *imageUrlArray;
@end

@implementation SEGActivityMyUploadCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    self.backgroundColor = kColor_W;

    //内容
    self.contentLabel = [[UILabel alloc] init];
    self.contentLabel.textColor = kColor_1;
    self.contentLabel.font = [UIFont systemFontOfSize:16];
    self.contentLabel.numberOfLines = 0;
    [self.contentView addSubview:self.contentLabel];
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.equalTo(self.contentView.mas_left).offset(15);
        make.right.equalTo(self.contentView.mas_right).offset(-15);
        make.top.equalTo(self.contentView).offset(15);
    }];
    
    self.imageContainer = [[UIView alloc] init];
    [self.contentView addSubview:self.imageContainer];
    [self.imageContainer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(0);
        make.top.equalTo(self.contentLabel.mas_bottom);
    }];

    //时间
    self.timeLabel = [[UILabel alloc] init];
    self.timeLabel.textColor = kColor_3;
    self.timeLabel.font = [UIFont systemFontOfSize:11];
    [self.contentView addSubview:self.timeLabel];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.right.mas_equalTo(-15);
        make.top.equalTo(self.imageContainer.mas_bottom).offset(15);
        make.bottom.mas_offset(-15);
    }];
}

- (void)setUpCellWithMyUploadModel:(SEGActivityMyUploadModel *)model
{
    self.contentLabel.text = model.quizContent;
    NSString *time = [SEGUtils stringDateByFormatString:@"yyyy-MM-dd HH:mm" withDate:[NSDate dateWithTimeIntervalSince1970:[model.createDate doubleValue]]];
    self.timeLabel.text = [NSString stringWithFormat:@"上传时间：%@",time];
    [self setImagesWithQuizPic:model.quizPic];
}

//设置图片
- (void)setImagesWithQuizPic:(NSString *)quizPic
{
    [self.imageContainer removeAllSubviews];
    if (!quizPic || quizPic.length == 0) {
        [self.imageContainer mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
        return;
    }
    
    CGFloat padding = 6.0;
    CGFloat imageWidth = floor((LYScreenWidth() - 30 - padding*2)/3.0);
    CGFloat topMargin = 9;
    
    NSArray *imageArray = [quizPic componentsSeparatedByString:@","];
    self.imageUrlArray = [NSMutableArray array];

    NSUInteger countInRow = imageArray.count == 4 ? 2 : 3; //一行最大图片个数
    [imageArray enumerateObjectsUsingBlock:^(NSString *imageString, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *imageUrlString =[NSString stringWithFormat:@"%@%@", kClientImageRootURL, imageString];
        [self.imageUrlArray addObject:imageUrlString];
        
        NSURL *url = [NSURL URLWithString:imageUrlString];
        UIImageView *imageView = [[UIImageView alloc] init];
        [imageView setImageWithURL:url placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"pic_default_300_300")]];
        imageView.tag = 10;
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.clipsToBounds = YES;
        
        UIButton *imageButton = [[UIButton alloc] init];
        imageButton.tag = 100 + idx;
        [imageButton addTarget:self action:@selector(seg_imageButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self.imageContainer addSubview:imageButton];
        [imageButton addSubview:imageView];
        
        
        CGFloat leftPadding = (idx % countInRow) * (imageWidth + padding);
        CGFloat topPadding = (idx / countInRow) * (imageWidth + padding) + topMargin;
        [imageButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(imageWidth);
            make.top.mas_equalTo(topPadding);
            make.left.mas_equalTo(leftPadding);
        }];
        
        
//        CGFloat leftPadding = (idx % countInRow) * (imageHeight + padding) + 15;
//        CGFloat topPadding = (idx / countInRow) * (imageHeight + padding) + 9;
//        if (idx == imageArray.count - 1) {
//            [imageButton mas_makeConstraints:^(MASConstraintMaker *make) {
//                make.width.height.mas_equalTo(imageHeight);
//                make.top.equalTo(self.contentLabel.mas_bottom).offset(topPadding).priorityHigh();
//                make.left.equalTo(self.contentView).offset(leftPadding);
//                make.bottom.equalTo(self.contentView).offset(-46);
//            }];
//            [self.timeLabel mas_remakeConstraints:^(MASConstraintMaker* make) {
//                make.right.equalTo(self.contentView.mas_right).offset(-15);
//                make.top.equalTo(imageButton.mas_bottom).offset(15);
//                // iOS10 以上的适配
//                if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0) {
//                    make.bottom.equalTo(self.contentView).mas_offset(-15).priorityHigh();
//                }
//            }];
//        }else {
//            [imageButton mas_makeConstraints:^(MASConstraintMaker *make) {
//                make.width.height.mas_equalTo(imageHeight);
//                make.top.equalTo(self.contentLabel.mas_bottom).offset(topPadding).priorityHigh();
//                make.left.equalTo(self.contentView).offset(leftPadding);
//            }];
//        }
        
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(imageButton);
        }];
    }];
    
    CGFloat imageContainerHeight = ceil(imageArray.count*1.0/countInRow)*(imageWidth + padding) + topMargin - padding;
    [self.imageContainer mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(imageContainerHeight);
    }];
}

- (void)seg_imageButtonPressed:(UIButton *)sender
{
    __block UIImageView *imageView;
    [sender.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.tag == 10) {
            imageView = (UIImageView *)obj;
        }
    }];
    if (self.delegate && [self.delegate respondsToSelector:@selector(myUploadCellImageTapWithImageUrlArray:SelectedImageView:SelectedIndex:)]) {
        [self.delegate myUploadCellImageTapWithImageUrlArray:self.imageUrlArray SelectedImageView:imageView SelectedIndex:sender.tag - 100];
    }
}
@end
